package org.example.controller.sysManage;

import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.controller.common.permission.PermissionObject;
import org.example.model.common.PageResponseDto;
import org.example.model.common.ResponseResult;
import org.example.model.user.dtos.PermissionDto;
import org.example.model.user.dtos.RoleInfoDto;
import org.example.model.user.vos.AddRoleInfoVo;
import org.example.model.user.vos.SearchRoleInfoVo;
import org.example.controller.common.permission.PermissionControl;
import org.example.service.user.RoleInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色信息表现层
 */
@CrossOrigin
@RestController
@RequestMapping("/role-info")
@Api(value = "角色信息接口", tags = {"角色信息接口"})
@PermissionControl(Parent = "系统配置", Name = "角色管理", Order = 3)
public class RoleInfoController {

    @Autowired
    private RoleInfoService roleInfoService;

    /**
     * 添加一条角色信息
     */
    @PostMapping
    @ApiOperation("添加一条角色信息")
    @PermissionControl(Parent = "角色管理", Name = "添加", Order = 1)
    public ResponseResult<Boolean> InsertRoleInfo(@RequestBody AddRoleInfoVo addRoleInfoVo) throws ClassNotFoundException, IllegalAccessException {
        return roleInfoService.InsertRoleInfo(addRoleInfoVo);
    }

    /**
     * 根据id修改一条角色信息
     */
    @PutMapping
    @ApiOperation("根据id修改一条角色信息")
    @PermissionControl(Parent = "角色管理", Name = "修改", Order = 3)
    public ResponseResult<Boolean> UpdateRoleInfoById(@RequestBody AddRoleInfoVo addRoleInfoVo){
        return roleInfoService.UpdateRoleInfoById(addRoleInfoVo);
    }

    /**
     * 分页根据条件（角色名，角色状态）获取角色信息列表
     */
    @GetMapping("/role-list")
    @ApiOperation("分页根据条件（角色名，角色状态）获取角色信息列表")
    @PermissionControl(Parent = "角色管理", Name = "查询", Order = 4)
    public ResponseResult<PageResponseDto<List<RoleInfoDto>>> SelectRoleInfoByConditions(SearchRoleInfoVo searchRoleInfoVo){
        return roleInfoService.SelectRoleInfoByConditions(searchRoleInfoVo);
    }

    /**
     * 根据id删除一条角色信息
     */
    @DeleteMapping("{id}")
    @ApiOperation("根据id删除一条角色信息")
    @PermissionControl(Parent = "角色管理", Name = "删除", Order = 2)
    public ResponseResult<Boolean> DeleteRoleInfoById(@PathVariable int id){
        return roleInfoService.DeleteRoleInfoById(id);
    }

    /**
     * 根据id获取当前角色的权限列表
     */
    @GetMapping("/permissions/{id}")
    @ApiOperation("根据id获取当前角色的权限列表")
    public ResponseResult<PermissionDto> GetPermissionListById(@PathVariable int id) throws JsonProcessingException, ClassNotFoundException, IllegalAccessException {
        return roleInfoService.GetPermissionListById(id);
    }

    /**
     * 根据id修改当前角色的权限列表
     */
    @PutMapping("/permissions")
    @ApiOperation("根据id修改当前角色的权限列表")
    @PermissionControl(Parent = "角色管理", Name = "权限设置", Order = 5)
    public ResponseResult<Boolean> UpdatePermissionListById(@RequestBody PermissionDto permissionDto) {
        return roleInfoService.UpdatePermissionListById(permissionDto);
    }

}
